package com.hncc.sms.controller;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.hncc.sms.domain.SysDeptManager;
import com.hncc.sms.service.ISysDeptManagerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 科室管理Controller
 *
 * @author ruoyi
 * @date 2020-08-08
 */
@RestController
@RequestMapping("/sms/dept")
@Api(tags = "科室管理接口")
public class SysDeptManagerController extends BaseController
{
    @Resource
    private ISysDeptManagerService sysDeptManagerService;

    /**
     * 查询科室管理列表
     */
    @ApiOperation(value = "条件分页查询科室管理",httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('sms:dept:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDeptManager sysDeptManager)
    {
        startPage();
        List<SysDeptManager> list = sysDeptManagerService.selectSysDeptManagerList(sysDeptManager);
        return getDataTable(list);
    }

    /**
     * 导出科室管理列表
     */
    @PreAuthorize("@ss.hasPermi('sms:dept:export')")
    @Log(title = "科室管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDeptManager sysDeptManager)
    {
        List<SysDeptManager> list = sysDeptManagerService.selectSysDeptManagerList(sysDeptManager);
        ExcelUtil<SysDeptManager> util = new ExcelUtil<SysDeptManager>(SysDeptManager.class);
        return util.exportExcel(list, "dept");
    }

    /**
     * 获取科室管理详细信息
     */
    @ApiOperation(value = "根据deptId获取科室管理详细信息",httpMethod = "GET",
            notes = "根据deptId查询科室管理")
    @ApiImplicitParam(paramType = "path",name = "deptId",value = "科室管理id",dataType = "Long",required = true)
    @PreAuthorize("@ss.hasPermi('sms:dept:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId)
    {
        return AjaxResult.success(sysDeptManagerService.selectSysDeptManagerById(deptId));
    }

    /**
     * 新增科室管理
     */
    @ApiOperation(value = "新增",httpMethod = "POST", notes = "新增科室管理")
    @ApiImplicitParam(name = "sysDeptManager", value = "新增科室管理", dataType = "SysDeptManager")
    @PreAuthorize("@ss.hasPermi('sms:dept:add')")
    @Log(title = "科室管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDeptManager sysDeptManager)
    {
        if(StringUtils.isNotNull(sysDeptManagerService.selectSysDeptManagerByDeptName(sysDeptManager.getDeptName()))){
            return AjaxResult.error("新增科室'" + sysDeptManager.getDeptName() + "'失败，科室名称已存在");
        }
        return toAjax(sysDeptManagerService.insertSysDeptManager(sysDeptManager));
    }

    /**
     * 修改科室管理
     */
    @ApiOperation(value = "修改", httpMethod = "PUT", notes = "修改科室管理")
    @ApiImplicitParam(name = "sysDeptManager", value = "修改科室管理", dataType = "SysDeptManager")
    @PreAuthorize("@ss.hasPermi('sms:dept:edit')")
    @Log(title = "科室管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDeptManager sysDeptManager)
    {
        SysDeptManager sysDeptManager1 = sysDeptManagerService.selectSysDeptManagerByDeptName(sysDeptManager.getDeptName());
        if(StringUtils.isNotNull(sysDeptManager1) && !sysDeptManager1.getCatId().equals(sysDeptManager.getCatId())){
            return AjaxResult.error("修改科室'" + sysDeptManager.getDeptName() + "'失败，科室名称已存在");
        }
        return toAjax(sysDeptManagerService.updateSysDeptManager(sysDeptManager));
    }

    /**
     * 删除科室管理
     */
    @ApiOperation(value = "批量删除", httpMethod = "DELETE", notes = "根据deptIds删除科室管理")
    @ApiImplicitParam(paramType = "path",name = "deptIds",value = "科室管理id",dataType = "Long[]",required = true)
    @PreAuthorize("@ss.hasPermi('sms:dept:remove')")
    @Log(title = "科室管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds)
    {
        return toAjax(sysDeptManagerService.deleteSysDeptManagerByIds(deptIds));
    }

    /**************************************************/
    /**
     * 查询科室所有分类
     * @return
     */
    /*@ApiOperation(value = "查询所有科室分类", httpMethod = "GET", notes = "查询所有科室分类")
    @GetMapping("/catId")
    public AjaxResult selectCatIdByList(){
        return AjaxResult.success(sysDeptManagerService.selectCatIdByList());
    }*/
    /**
     * 导入科室数据
     */
    @Log(title = "科室管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sms:dept_manager:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<SysDeptManager> util = new ExcelUtil<>(SysDeptManager.class);
        List<SysDeptManager> deptList = util.importExcel(file.getInputStream());
        String message = sysDeptManagerService.importUser(deptList,updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate(){
        ExcelUtil<SysDeptManager> util = new ExcelUtil<SysDeptManager>(SysDeptManager.class);
        return util.importTemplateExcel("科室数据");
    }
}
